package com.stars.controller.sys;

import com.github.pagehelper.PageInfo;
import com.stars.common.annotation.Log;
import com.stars.common.annotation.UserMenuUpdateRedis;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.enums.OperationType;
import com.stars.common.result.VueTreeItem;
import com.stars.controller.BaseController;
import com.stars.entity.command.sys.SystemPermissionCommand;
import com.stars.entity.dto.sys.SystemPermissionDTO;
import com.stars.entity.sys.SystemPermission;
import com.stars.services.system.PermissionAccessService;
import com.stars.services.system.PermissionConfigService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : alex
 * @version ：1.0.0
 * @Date : create by 2017/12/5 18:00
 * @description ：资源控制层
 * @note 注意事项
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "资源控制层")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionConfigService permissionConfigServiceImpl;
    @Autowired
    private PermissionAccessService permissionAccessServiceImpl;

    /**
     * 添加资源
     *
     * @param command
     * @command
     * @throws Exception
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加资源",notes = "添加资源")
    @Log(detail = "添加更新资源"+ BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg permissionInsert(@Valid SystemPermissionCommand command,HttpServletRequest request) throws Exception {
        addByCommand(command,request);
        return permissionConfigServiceImpl.permissionInsert(command);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @PostMapping("/permissionList")
    @ApiOperation(value = "分页查询",notes = "分页查询")
    public PageInfo<SystemPermissionDTO> permissionList(Integer pageNum, Integer pageSize, SystemPermissionDTO command) throws
            Exception {
        PageInfo<SystemPermissionDTO> page = permissionAccessServiceImpl.findByPage(pageNum, pageSize, command);
        return page;
    }

    /**
     * 获取全部的资源数据
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/permissionTree")
    @ApiOperation(value = "获取全部的资源数据",notes = "获取全部的资源数据")
    public ResultMsg permissionTree() throws Exception {
        List<VueTreeItem> byAll = permissionAccessServiceImpl.findByAll();
        return ResultMsg.success(byAll);
    }

    @GetMapping("/findByPid")
    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    public ResultMsg findByPid(String parentId)throws Exception{
        List<SystemPermissionDTO> systemPermissionList =  permissionAccessServiceImpl.findByPid(parentId);
      return ResultMsg.success(systemPermissionList);
    }
    /**
     * 根据菜单类型查询资源。
     * @param perType 菜单类型
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/findPermissionByPerType")
    @ApiOperation(value = "根据菜单类型查询资源",notes = "根据菜单类型查询资源")
    public ResultMsg findPermissionByPerType(String perType)throws Exception{
        List<SystemPermissionDTO> list =  permissionAccessServiceImpl.findPermissionByPerType(perType);
        return ResultMsg.success(list);
    }
    /**
     * 根据id查询资源
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "根据id查询资源",notes = "根据id查询资源")
    public ResultMsg findById(String id)throws Exception{
        SystemPermission systemPermission = permissionAccessServiceImpl.findByEntityId(id);
        return ResultMsg.success(systemPermission);
    }

    @PostMapping(value = "/delById")
    @ApiOperation(value = "根据id删除资源",notes = "根据id删除资源")
    public ResultMsg delById(String ids)throws Exception{

        if(StringUtils.isNotBlank(ids)){
            List<String> msgList = new ArrayList<>();
            String [] idsArray = ids.split(BaseConstant.SPOT);
            for(String id:idsArray){
                List<SystemPermissionDTO> systemPermissionList =  permissionAccessServiceImpl.findByPid(id);
                if(null != systemPermissionList && systemPermissionList.size()>0){
                    msgList.add("存在子菜单，不能删除");
                    break;
                }
            }
            return permissionConfigServiceImpl.deleteByIds(idsArray);
        }
        return ResultMsg.failure("删除失败");
    }


}
